<?php
class IndexAction extends CommonAction {
	// 框架首页
	public function index() {
		ob_clean();
		$this->_checkUser();
		$this->_Config_name();//调用参数
		C ( 'SHOW_RUN_TIME', false ); // 运行时间显示
		C ( 'SHOW_PAGE_TRACE', false );

		/*for($i = 0; $i < 120; $i++) {
		    $this->get_curr_time_section();
        }*/


        $this->autoEdayPresent();           //周期配币
        //$this->autoAddMoneyPackAddress();   //添加钱包地址
		//$this->autoPresentMoney();
        //$this->autoEdayPresent();
		$fck = D ('Fck');
/*****************************************/
    // $fee_rs = M('fee')->find();
    // $p_path = $fck->where('id>0 and b10<1')->field('id,p_path,cpzj')->order('p_level asc,id desc')->select();
    // foreach ($p_path as $key => $vv) {

    //   $fck->query("UPDATE `xt_fck` SET `zjj`=zjj+".$vv['cpzj']." WHERE id in (0".$vv['p_path']."0)");
    //   $fck->query("UPDATE `xt_fck` SET `b10`=1 WHERE id =".$vv['id']."");
    //    $fck->query("UPDATE `xt_fck` SET `is_bb`=0 WHERE id =".$vv['id']."");

    //       $zjj = M('fck')->where('id in(0'.$vv['p_path'].'0) and is_pay>0')->order('p_level desc')->select();
    //     //增加总业绩+升级
    //     foreach ($zjj as $key => $value) {
    //         if($value['is_bb']<1){
           
    //             //查询团队业绩
    //             $l = M('fck')->where('father_id='.$value['id'].' and treeplace=0')->field('cpzj,zjj')->find();//左区
    //             $l = $l['cpzj']+$l['zjj'];
    //             $r = M('fck')->where('father_id='.$value['id'].' and treeplace=1')->field('cpzj,zjj')->find();//右区
    //             $r = $r['cpzj']+$r['zjj'];
    //             $lr = M('fck')->where('father_id='.$value['id'].' and treeplace=2')->field('cpzj,zjj')->find();//三区
    //             $lr = $lr['cpzj']+$lr['zjj'];
    //             $array=array($l,$r,$lr);
    //              rsort($array);
    //             $min = min($array[0],$array[1]);//三区第二小业绩                      
    //             $yeji = $s12[0]*10000;
    //             if($value['zjj']>=$yeji && $min>=$fee_rs['s5']){
    //                 $data['is_bb'] = 1;
    //                 M('fck')->where('id='.$value['id'])->save($data);
    //             }//领导升级（主任）

    //          }else{
    //              //查询团队业绩
    //             $l = M('fck')->where('father_id='.$value['id'].' and treeplace=0')->field('id')->find();//左区
    //             $l = M('fck')->where('p_path like "%'.$l['id'].'%" or id='.$l['id'])->field('is_bb')->order('is_bb desc')->find();//左区团队最小级别

    //             $r = M('fck')->where('father_id='.$value['id'].' and treeplace=1')->field('id')->find();//右区
    //             $r = M('fck')->where('p_path like "%'.$r['id'].'%" or id='.$r['id'])->field('is_bb')->order('is_bb desc')->find();
    //             $lr = M('fck')->where('father_id='.$value['id'].' and treeplace=2')->field('id')->find();//三区
    //             $lr = M('fck')->where('p_path like "%'.$lr['id'].'%" or id='.$lr['id'])->field('is_bb')->order('is_bb desc')->find();
    //             $array=array($l,$r,$lr);
    //              rsort($array);
    //              // if($value['user_id']==15357433205){echo $array[1]['is_bb'];echo "<br />";}
    //              if($array[1]['is_bb']>=$value['is_bb']){
    //                 $yeji = $value['is_bb']*10000;
    //                 if($value['zjj']>=$yeji){
    //                     $data['is_bb'] = $value['is_bb']+1;
    //                     if($data['is_bb']>8){
    //                         $data['is_bb'] = 8;
    //                     }
    //                     M('fck')->where('id='.$value['id'])->save($data);
    //                 }

    //              }
    //          }

             
    //     }
    // }

/*****************************************/
    $nowdate = strtotime(date('Y-m-d'));
    //清空每日时间
    $fck->query("UPDATE `xt_fck` SET `day_feng`=0,_times=".$nowdate." WHERE _times !=".$nowdate."");
    

    // $fck->quanqiu();//董事分红
    $fcks=D('Fck');
    $bid = $fcks->_getTimeTableList(0,1);

    /*$money = $fcks->y_fenh($bid);       //判断是否进行月分红
    $fcks->dsqgfenh($money);             //董事全球分红
    $fcks->zjqgfenh($money);             //总监全球分红
    $fcks->Q_Spand();                   //重复消费清0*/



		$id = $_SESSION[C('USER_AUTH_KEY')];

		$field = '*';
		$fck_rs = $fck -> field($field) -> find($id);

		$HYJJ="";
		$this->_levelConfirm($HYJJ,1);
    //===========================开始发放月补贴==========================
    // if($fck_rs['is_fenh']==0){
    //   $fck->mybt($fck_rs['id'],$fck_rs['is_aa'],$fck_rs['my_pay_num'],$fck_rs['u_level'],$fck_rs['re_path'],$fck_rs['user_id']);
    // }
		$this->assign('voo',$HYJJ);//会员级别

    $fck_rs = $fck -> field($field) -> find($id);
		$this->assign('fck_rs',$fck_rs);

        //滚动
        $fee_rs = M('fee')->field('str4')->find();
        $this->assign('roll',$fee_rs['str4']);

		$this->display();
	}
	//用户协议的判断
    public function get_xieyi(){
		$_SESSION['modal_agreement']=0;
	}
	public function quXiao(){
		$_SESSION=array();
	}

	 public function yanzheng1() {




        $code=$_GET['code'];
      

        $url="https://api.weixin.qq.com/sns/oauth2/access_token?";
        $post_data="appid=wxdfc6fec1902c9870&secret=2f6643d8d42a16646da60953e61f2af2&code=".$code."&grant_type=authorization_code";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false );
       
        $result = curl_exec($ch);
         if(curl_errno($ch)){
            // 显示报错信息；终止继续执行
            die(curl_error($ch));
        }
        curl_close($ch);
         $result2 = json_decode($result,true);

        $dd=$result2['access_token'];
        $pid=$result2['openid'];
        $_SESSION['login_count']        =   $pid;
        $_SESSION['login_isAgent']      =   $dd;
    
   
  
      
      
     
        $url1="https://api.weixin.qq.com/sns/userinfo?";
        $post_data1="access_token=".$dd."&openid=".$pid."&lang=zh_CN";
        $ch1 = curl_init();
        curl_setopt($ch1, CURLOPT_POST, 1);
        curl_setopt($ch1, CURLOPT_HEADER, 0);
        curl_setopt($ch1, CURLOPT_URL, $url1);
        curl_setopt($ch1, CURLOPT_POSTFIELDS, $post_data1);
        curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt ($ch1, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt ($ch1, CURLOPT_SSL_VERIFYPEER, false );
       
        $result1 = curl_exec($ch1);
       $data = json_decode($result1,true);

      $fck=M('fck');
      $time=time();
     

      $a=$fck->where("user_id='".$pid."'")->find();
      if(!$a){

       // $aid=$_SESSION['loginUseracc'];
       

          $tt=array();
          $tt['user_id']=$pid;
          $tt['nickname']=$data['nickname'];
          $tt['get_ceng']=$data['sex'];
          $tt['us_img']=$data['headimgurl'];
          $tt['u_level']=0;
          $tt['pdt']    =$time;
          $tt['new_login_time']=$time;
          $rs=$fck->add($tt);
          if($rs){
            $aa=$fck->where("user_id='".$pid."'")->find();
            $_SESSION[C('USER_AUTH_KEY')]    =   $aa['id']; 
          }
        }
        
    else{
       $fck->execute("update __TABLE__ set nickname='".$data['nickname']."',us_img='".$data['headimgurl']."',get_ceng=".$data['sex'].",new_login_time=".$time." where user_id='".$pid."'");
      
       $_SESSION[C('USER_AUTH_KEY')]    =   $a['id'];
      
        
    }

   $this->index();

    } 

	public function Service(){
	    $fee_rs = M('fee')->find();
	    $this->assign('fee_rs',$fee_rs);
	    $this->display();
    }

    public function home() {
	    $this->display();
    }

    /**
     *  添加钱包地址
     */
    public function autoAddMoneyPackAddress() {
        $null_add_pack = M('fck')->where('weixinCode is null or weixinCode="" ')
            ->field('id,user_id,weixinCode')->select();
        foreach ($null_add_pack as $rs) {
            $code =  $this->createAddrRandom();
            $data['weixinCode'] = $code;
            M('fck')->where(['id' => $rs['id']])->save($data);
        }
        sleep(0.7);   //等待执行, 已避免执行过快
    }

    public function test() {
        /*$r = new \Think\Page(25, 12);
        dump($r);*/
        import('ORG.Util.Page');
        $p = new \Think\Page(25, 15);
        dump($p);
        exit;
    }

    public function testColor() {
        $i = imagecreatefrompng( $_SERVER['DOCUMENT_ROOT']. __ROOT__."/Public/Images/add2.png");
        $l = $_SERVER['CONTEXT_DOCUMENT_ROOT'] .  "/Public/Images/header.png";
        $op = 'https://avatar.csdn.net/F/2/E/3_fanblog.jpg';
        $i = ImageCreateFromjpeg( $op);

        dump($i);
        for ($x=0;$x<imagesx($i);$x++) {
            for ($y=0;$y<imagesy($i);$y++) {
                $rgb = imagecolorat($i,$x,$y);
                $r  = ($rgb >> 16) & 0xFF;
                $g  = ($rgb >> 8) & 0xFF;
                $b  = $rgb & 0xFF;
                $rTotal += $r;
                $gTotal += $g;
                $bTotal += $b;
                $total++;
            }
        }
        $rAverage = round($rTotal/$total);
        $gAverage = round($gTotal/$total);
        $bAverage = round($bTotal/$total);


    }


}
?>